package com.sagereal.wtc.factorymode.Activity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.widget.Toast;

import com.sagereal.wtc.factorymode.R;

public class KeyActivity extends BaseActivity {
    Button mVolumeUp,mVolumeDown,mPower,mTestPass,mTestFail;
    private boolean mKeyUp = false,mKeyDown = false,mKeyPower = false;
    private PowerButtonReceiver mPowerButtonReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_key);
        setTitle(getString(R.string.title_key_test));

        mVolumeUp = findViewById(R.id.btn_volumeUp);
        mVolumeDown = findViewById(R.id.btn_volumeDown);
        mPower = findViewById(R.id.btn_power);
        mTestPass = findViewById(R.id.test_pass);
        mTestFail = findViewById(R.id.test_fail);

        // 注册广播接收器监听电源键
        mPowerButtonReceiver = new PowerButtonReceiver();
        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_SCREEN_ON);
        registerReceiver(mPowerButtonReceiver, filter);

        // 初始化按键状态
        initView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 取消注册广播接收器
        if (mPowerButtonReceiver != null) {
            unregisterReceiver(mPowerButtonReceiver);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_VOLUME_UP:
                handleVolumeUp();
                return true;
            case KeyEvent.KEYCODE_VOLUME_DOWN:
                handleVolumeDown();
                return true;
            case KeyEvent.KEYCODE_POWER:
                handlePower();
                return true;
            default:
                return super.onKeyDown(keyCode, event);
        }
    }

    private void handleVolumeUp() {
        if (mVolumeUp != null) {
            mVolumeUp.setVisibility(View.INVISIBLE);
            mKeyUp = true;
        }
    }

    private void handleVolumeDown() {
        if (mVolumeDown != null) {
            mVolumeDown.setVisibility(View.INVISIBLE);
            mKeyDown = true;
        }
    }

    private void handlePower() {
        if (mPower != null) {
            mPower.setVisibility(View.INVISIBLE);
            mKeyPower = true;
        }
    }

    private void initView() {
        mTestPass.setOnClickListener(v -> {
            if(mKeyUp && mKeyDown && mKeyPower){
                Intent intent = new Intent();
                intent.putExtra("ItemId", getIntent().getIntExtra("ItemId", -1));
                intent.putExtra("status", "pass");
                intent.putExtra("check", getIntent().getStringExtra("check"));
                setResult(RESULT_OK,intent);
                finish();
            }else {
                // 弹出提示
                mTestPass.setEnabled(false);
                Toast.makeText(KeyActivity.this, getString(R.string.key_passbtn_notify), Toast.LENGTH_SHORT).show();
                new Thread(() -> {
                    try {
                        Thread.sleep(3000); // 等待Toast显示时间
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    // 重新启用按钮
                    runOnUiThread(() -> mTestPass.setEnabled(true));
                }).start();
            }
        });

        mTestFail.setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.putExtra("ItemId", getIntent().getIntExtra("ItemId", -1));
            intent.putExtra("status", "fail");
            intent.putExtra("check", getIntent().getStringExtra("check"));
            setResult(RESULT_OK , intent);
            finish();
        });
    }

    private class PowerButtonReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
                handlePower();
            }
        }
    }
}
